(function() {
  var ADIMG_URL = '/api/adimages'
  var HOTCOURSES_URL = '/api/products?isHot=1'
  var TEACHERS_URL = '/api/teachers'
  var INTRO_URL = '/api/agency'



  function loadBanner(data) {
    var htmlStr = ''
    $.each(data, function (i, item) {
      htmlStr += '<li class="swiper-slide">\
        <a href="'+ item.url +'">\
          <img src="'+ item.image +'" alt="" />\
        </a>\
      </li>'
    })

    $('#J_swiper').html(htmlStr)

    var mySwiper = new Swiper('.swiper-container',{
      pagination: '.swiper-pagination',
      slidesPerView: 1,
      paginationClickable: true,
      autoplayDisableOnInteraction: false,
      spaceBetween: 30,
      keyboardControl: true,
      nextButton: '.swiper-button-next',
      prevButton: '.swiper-button-prev',
      paginationClickable: true,
      loop: true,
      // autoplay: false,
      autoplay: 2000,
    });
  }

  function loadHotCourses(data) {

    var htmlStr = ''
    $.each(data.list, function (i, item) {
      htmlStr += '<dl>\
        <dt><img src="'+ item.cover +'"/></dt>\
        <dd>\
          <p class="home_courser_one">'+ item.title +'</p>\
          <p class="home_courser_two">\
            <span class="home_courser_two_span">教师:<a>'+ (item.masterinfo ? item.masterinfo.lastname : '') +'</a></span>\
            <i class="courser_yellow">'+ (item.price === '0.00'? '免费':('￥'+item.price))+ '</i>\
          <p>\
          <p class="home_courser_three" style="clear: both;margin-left:14px;">\
            <span>浏览:<a class="browse">'+ item.visited +'</a>　购买:<a class="buy">'+ item.sold +'</a></span><button>购买</button>\
          <p>\
        </dd>\
      </dl>'
    })

    $('#J_hotcourse').html(htmlStr)
  }

  function loadTeacher(data) {
    var htmlStr = ''
    $.each(data.list, function (i, item) {
      var showClass = ''
      if(!item.coursedesc.length) {
        showClass = 'invisible'
      }

      htmlStr += '<dl>\
        <dt><img src="'+ item.photo +'"/></dt>\
        <dd>\
          <p>'+ item.lastname +'</p>\
          <p class="'+ showClass +'">('+ $.map(item.coursedesc, function (i, item) {
            return item.coursetypename
          }).join(', ') +')</p>\
          <div>\
              <p style="height:38px;"><span>简介:</span>'+ item.introduce +'</p>\
          </div>\
        </dd>\
      </dl>'
    })

    $('#J_teachers').html(htmlStr)
  }

  function loadAgency(data) {
    $('#J_custom').append('<img src="'+ data.image +'" alt="" />')
  }

  $.when(
    $.ajax(ADIMG_URL),
    $.ajax(HOTCOURSES_URL),
    $.ajax(TEACHERS_URL),
    $.ajax(INTRO_URL)
  ).done(function () {
    var cbFnArr = [
      loadBanner,
      loadHotCourses,
      loadTeacher,
      loadAgency
    ]

    var resArr = [].slice.call(arguments,0,4)
    $.each(resArr, function (i, curRes) {
      if(curRes[1] == 'success') {
        cbFnArr[i](curRes[0].data)
      }
    })
  })
}());
